package com.example.PolimiAssistant.utils;

import android.util.Log;
import com.Wsdl2Code.WebServices.PolimiAssistant.*;

import java.util.*;

/**
 * AccountUtils.java
 *
 * This class manages exposes some query methods in order to manipulate
 * the information from the account system in android.
 */
public class UserDataManager {

    /**
     * The tag used for log
     */
    private static final String TAG = UserDataManager.class.getSimpleName();


    /**
     * This method, returns the user information profile. This methods assumes
     * that the user already made the login therefore only requires the
     * oAuthentication token.
     *
     * @param token
     * @return
     */
    public static UserProfile getUserProfile(String token){

        //1. Call the polimi web service methods
        PolimiAssistant polimiService;
        polimiService = new PolimiAssistant();

        //2. Call the polimi login service to obtain the user profile
        UserProfile poliUser = null;

        try {

            poliUser = polimiService.GetUserProfile(token);

        }catch (Exception e){
            Log.i(TAG, "The app could not connect to the web service");
        }


        return poliUser;

    }

    /**
     * This method, returns the user information profile. The method is used mainly
     * for the token retrieval.
     *
     * @param username
     * @param password
     * @return
     */
    public static PoliUser login(String username, String password){

        //1. Call the polimi web service methods
        PolimiAssistant polimiService;
        polimiService = new PolimiAssistant();

        //2. Call the polimi login service to obtain the user account data
        PoliUser poliUser = null;

        try {

            poliUser = polimiService.Login(username, password);

        }catch (Exception e){
            Log.i(TAG, "The app could not connect to the web service");
        }

        return poliUser;
    }

    public static Map<String, List<CourseExamInfo>> getActiveAndCompletedCourses(UserProfile userProfile){

        Map<String, List<CourseExamInfo>> courses = new HashMap<String, List<CourseExamInfo>>();
        List<CourseExamInfo> activeCourses = new ArrayList<CourseExamInfo>();
        List<CourseExamInfo> passedCourses = new ArrayList<CourseExamInfo>();

        //1. Call the polimi web service methods
        PolimiAssistant polimiService;
        polimiService = new PolimiAssistant();

        //2. Get the whole list of courses
        List<CourseExamInfo> allCourses = userProfile.user_courses;

        //3. Filter the courses to generate the two lists
        Iterator<CourseExamInfo> i = allCourses.iterator();

        while(i.hasNext()){

            CourseExamInfo course = i.next();

            Log.i(TAG, "The following course will de added to the collection: " +
                    "Name: " + course.course.name
                    + " , grade: " + course.grade
                    + " , id: " + course.course.credits);

            if(course.grade < 18){
                activeCourses.add(course);
            }else{
                passedCourses.add(course);
            }

        }

        //4. Add the courses to the final list
        courses.put("Active Courses", activeCourses);
        courses.put("Passed Courses", passedCourses);

        return courses;
    }

    public static Map<String,ArrayList<UserCurrentSchedule>> getAcademicSchedule(String token) {

        Map<String, ArrayList<UserCurrentSchedule>> scheduleList = new HashMap<String, ArrayList<UserCurrentSchedule>>();
        ArrayList<UserCurrentSchedule> monday = new ArrayList<UserCurrentSchedule>();
        ArrayList<UserCurrentSchedule> tuesday = new ArrayList<UserCurrentSchedule>();
        ArrayList<UserCurrentSchedule> wednesday = new ArrayList<UserCurrentSchedule>();
        ArrayList<UserCurrentSchedule> thursday = new ArrayList<UserCurrentSchedule>();
        ArrayList<UserCurrentSchedule> friday = new ArrayList<UserCurrentSchedule>();
        ArrayList<UserCurrentSchedule> allSchedules;

        //1. Call the polimi web service methods
        PolimiAssistant polimiService;
        polimiService = new PolimiAssistant();

        //2. Get the whole schedule
        allSchedules = new ArrayList<UserCurrentSchedule>(polimiService.GetUserCurrentSchedule(token));

        //3. Filter the schedule to each one of the lists
        Iterator<UserCurrentSchedule> i = allSchedules.iterator();

        while(i.hasNext()){

            UserCurrentSchedule course = i.next();

            //Filters
            if(course.day == 1){
                monday.add(course);

            }else if(course.day == 2){
                tuesday.add(course);

            }else if(course.day == 3){
                wednesday.add(course);

            }else if(course.day == 4){
                thursday.add(course);

            }else if(course.day == 5){
                friday.add(course);

            }else {
                //report error
            }
        }

        scheduleList.put("Monday", monday);
        scheduleList.put("Tuesday", tuesday);
        scheduleList.put("Wednesday", wednesday);
        scheduleList.put("Thursday", thursday);
        scheduleList.put("Friday", friday);

        return scheduleList;
    }
}
